package com.cloudsigns.factorymanager2.produce.model;

import android.util.Log;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartFeedbackResponse;
import com.cloudsigns.factorymanager2.produce.utils.ParseJson;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ProductFeedbackListModel implements IProductFeedbackListModel {

    @Override
    public void productFeedbackList(final OnProductFeedbackListListener onProductFeedbackListListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpClient client = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(ProjectUtil.PRODUCT_FEEDBACK_LIST);
                HttpResponse response;
                try {
                    httpPost.addHeader("token", MyApplicaton.getApplication().getToken());
                    httpPost.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);
                    response = client.execute(httpPost);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    Log.e("ProductFeedbackListMode", "ProjectUtil=" + ProjectUtil.PRODUCT_FEEDBACK_LIST);
                    Log.e("ProductFeedbackListMode", "result=" + result);
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");
                    if (null != onProductFeedbackListListener) {
                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            JSONArray jsonArr = new JSONArray(json.getString("workPartFeedbackResponseList"));
                            List<WorkPartFeedbackResponse> workPartFeedbackResponseList = new ArrayList<>();
                            if (jsonArr != null) {
                                for (int i = 0; i < jsonArr.length(); i++) {
                                    JSONObject jsonObj = jsonArr.getJSONObject(i);
                                    if (jsonObj != null) {
                                        WorkPartFeedbackResponse workPartFeedbackResponse = new WorkPartFeedbackResponse();
                                        workPartFeedbackResponse.setEngineeringName(jsonObj.getString("engineeringName"));
                                        workPartFeedbackResponse.setOrderId(jsonObj.getString("orderId"));
                                        workPartFeedbackResponse.setProductId(jsonObj.getString("productId"));
                                        workPartFeedbackResponse.setProductName(jsonObj.getString("productName"));
                                        workPartFeedbackResponse.setProjectName(jsonObj.getString("projectName"));
                                        CommonEnum.FeedbackStatus status = ParseJson.parseFeedbackStatus(jsonObj, "status");
                                        workPartFeedbackResponse.setStatus(status);
                                        CommonEnum.WorkPartType type = ParseJson.parseFeedbackType(jsonObj, "type");
                                        workPartFeedbackResponse.setType(type);
                                        workPartFeedbackResponse.setWorkPartFeedbackId(jsonObj.getString("workPartFeedbackId"));
                                        workPartFeedbackResponse.setWorkPartId(jsonObj.getString("workPartId"));
                                        workPartFeedbackResponse.setWorkPartName(jsonObj.getString("workPartName"));
                                        workPartFeedbackResponse.setWorkPartOrderId(jsonObj.getString("workPartOrderId"));

                                        workPartFeedbackResponseList.add(workPartFeedbackResponse);
                                    }
                                }
                                onProductFeedbackListListener.getSuccessed(workPartFeedbackResponseList);
                            }
                        } else {
                            onProductFeedbackListListener.getFailed(code + "");
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }
            }
        }).start();
    }

    public interface OnProductFeedbackListListener {

        void getSuccessed(List<WorkPartFeedbackResponse> workPartFeedbackResponseList);

        void getFailed(String desc);
    }
}
